/**
 * Created by Baggio on 2017-5-19.
 */
$(function () {
    $(".videoRecommend_img_0").click(function () {
        $this = $(this);
        var shoppingCartId = $this.attr("data-shoppingCartId");
        var  isCheck=$this.attr("data-isCheck");
        if (isCheck==1){
            $(this).find("img").attr("src","/static/images/icon/no.png");
            $this.attr("data-isCheck","");
        }
        else {
            $(this).find("img").attr("src","/static/images/icon/yes.png");
            $this.attr("data-isCheck", 1);
        }
      //  $(".videoRecommend_img_0").attr("data-isCheck", "");
    });
    $("#toPay").click(function () {
        var number = $("#buyNum").val();
        if (number > 10 || number < 1) {
            alert("你输入的课程数量必须在1-10之间");
            return;
        }

        var classIds = "";
        var productJsons = [];

        $(".videoRecommend_img_0").each(function () {
            if ($(this).attr("data-isCheck") == 1) {
             var buyNum=   $(this).parent().find(".cartNum").find("span:eq(1)").html();
                var productJson = {
                    classId: $(this).attr("data-classId"),
                    buyNum: buyNum
                };
                console.log(productJson);
                productJsons.push(productJson);
            }

        });

        var activityIds = $("#activityIds").val().split(",");
        console.log(activityIds)
        var activityList = [];

        for (var i = 0; i < activityIds.length; i++) {


            if (activityIds[i] =="" ||  activityIds[i] == null || activityIds[i] == undefined) {
                alert(activityId);
                continue;
            }
            var activityId = {activityId: activityIds[i]};
            activityList.push(activityId)
        }
        createOrder(1, productJsons, true, 2, activityList);
    });
    $("#deleteShoppingCart").click(function () {

        $(".videoRecommend_img_0").each(function () {
            if ($(this).attr("data-isCheck") == 1) {
                var shoppingCartId = $(this).attr("data-shoppingCartId");
                var  postData={shoppingCartId:shoppingCartId};
                $.ajax({
                    type: 'post',
                    url: '/shoppingCart/cancelShoppingCart.html',
                    data: postData,
                    success: function (json) {
                        var data = JSON.parse(json); //由JSON字符串转换为JSON对象
                        console.log(data);
                        if (data.code != 1) {
                            alert(data.msg);
                            return;
                        }
                        alert("删除成功！");
                        location.reload();
                    }
                });
            }
        });

    });


});
function addAoumt(shoppingCartId,$this) {

    $obj= $this.parent().parent().find("span:eq(1)");
    var cartNum = $obj.html() * 1;
    cartNum += 1;
    if (cartNum > 10) {
        return;
    }
    $obj.html(cartNum);
    //$("#cartNum").html(cartNum);
    modifyCart(shoppingCartId, cartNum);
}
function subAoumt(shoppingCartId,$this) {

    $obj= $this.parent().parent().find("span:eq(1)");
    var cartNum = $obj.html() * 1;
    if (cartNum < 2) {
        return;
    }
    cartNum -= 1;
    $obj.html(cartNum);
    modifyCart(shoppingCartId, cartNum);
}

function modifyCart(shoppingCartId, productCount) {
    var data = {tid: shoppingCartId, amount: productCount};
    $.ajax({//获取用户id

        type: 'post',
        url: '/shoppingCart/modifyShoppingCart.html',
        data: data,
        success: function (json) {
               var data = JSON.parse(json); //由JSON字符串转换为JSON对象
             console.log(data);
            if (data.code != 1) {
                alert(data.msg);
                return;
            }
            alert("修改成功！")
          // getCount();

        }
    });
}